﻿// <copyright file="FileGeodatabaseFilterWrapper.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>Implementation of FileGeodatabaseFilterWrapper class.</summary>

namespace BESASM.EMGAATS.Arc
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using ESRI.ArcGIS.Catalog;
  using ESRI.ArcGIS.DataSourcesGDB;
  using ESRI.ArcGIS.Geodatabase;

  /// <summary>
  /// A class wrapping the default <see cref="GxFilterFileGeodatabases"/> class.
  /// </summary>
  public class FileGeodatabaseFilterWrapper : IGxObjectFilter
  {
    private IGxObjectFilter internalFilter;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileGeodatabaseFilterWrapper"/> class.
    /// </summary>
    public FileGeodatabaseFilterWrapper()
    {
      this.internalFilter = new GxFilterFileGeodatabasesClass();
    }

    #region IGxObjectFilter Members

    /// <summary>
    /// Gets a string that describes what this filter does.
    /// </summary>
    public string Description
    {
      get
      {
        return this.internalFilter.Description;
      }
    }

    /// <summary>
    /// Gets a user-friendly name identifying this filter.
    /// </summary>
    public string Name
    {
      get
      {
        return this.internalFilter.Name;
      }
    }

    /// <summary>
    /// Indicates if the given object can be chosen.
    /// </summary>
    /// <param name="object">The object to be chosen.</param>
    /// <param name="result">The result of double-clicking on the object.</param>
    /// <returns>True if the object can be chosen, otherwise false.</returns>
    public bool CanChooseObject(IGxObject @object, ref esriDoubleClickResult result)
    {
      bool response = this.internalFilter.CanChooseObject(@object, ref result);
      result = esriDoubleClickResult.esriDCRChooseAndDismiss;
      return response;
    }

    /// <summary>
    /// Indicates if the given object can be displayed.
    /// </summary>
    /// <param name="object">The object to display.</param>
    /// <returns>True if the object can be displayed, otherwise false.</returns>
    public bool CanDisplayObject(IGxObject @object)
    {
      return this.internalFilter.CanDisplayObject(@object);
    }

    /// <summary>
    /// Indicates if a new object named newObjectName could be saved in the specified location.
    /// </summary>
    /// <param name="location">The location where the new object is to be created.</param>
    /// <param name="newObjectName">The name of the new object.</param>
    /// <param name="objectAlreadyExists">A value indicating if an object already exists at this location.</param>
    /// <returns>True if the object can be saved, otherwise false.</returns>
    public bool CanSaveObject(IGxObject location, string newObjectName, ref bool objectAlreadyExists)
    {
      bool response = this.internalFilter.CanSaveObject(location, newObjectName, ref objectAlreadyExists);

      string newPath = Path.Combine(location.FullName, newObjectName);
      IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
      objectAlreadyExists = workspaceFactory.IsWorkspace(newPath);

      return response;
    }

    #endregion
  }
}
